import 'package:flutter/material.dart';
import 'package:x3_charts_core/chart_painter/layered_cake_pie_char_painter.dart';
import 'package:x3_charts_core/model/layered_cake_pie_chart_model.dart';

///**
///* @author hrongyong
///* @date   3/4/21
///* @email hrongyong@gmail.com
///* Description: 分层蛋糕饼图
///*/
class LayeredCakePieCharWidget extends StatefulWidget {
  final List<LayeredCakePieChartModel> chartList;

  ///背景颜色
  final Color backgroundColor;

  ///默认饼块颜色
  final Color defaultColor;

  ///边距大小 - 用于调整四周的边距预留位置 width:横向边距 height：纵向边距
  final Size marginSize;

  LayeredCakePieCharWidget({
    @required this.chartList,
    this.backgroundColor = Colors.white,
    this.defaultColor = Colors.lightBlueAccent,
    this.marginSize,
  });

  @override
  _LayeredCakePieCharWidgetState createState() => _LayeredCakePieCharWidgetState();
}

class _LayeredCakePieCharWidgetState extends State<LayeredCakePieCharWidget> with SingleTickerProviderStateMixin {
  AnimationController _controller;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(milliseconds: 1000),
      vsync: this,
    )..forward();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: LayoutBuilder(
        builder: (BuildContext context, BoxConstraints constraints) {
          return CustomPaint(
            size: Size(constraints.maxWidth, constraints.maxHeight),
            painter: LayeredCakePieCharPainter(
              chartList: widget.chartList,
              repaint: _controller,
              backgroundColor: widget.backgroundColor,
              defaultColor: widget.defaultColor,
              marginSize: widget.marginSize,
            ),
          );
        },
      ),
    );
  }
}
